package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.date.DateUtils;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.OrdersettingMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.poi.POIUtils;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrdersettingService;
import com.itheima.service.SetmealService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class OrdersettingServiceImpl extends ServiceImpl<OrdersettingMapper, OrderSetting> implements OrdersettingService {

    @Override
    public void importOrderSettings(List<String[]> list) {
//        目前有以下几个问题需要注意:
//            1,excle表格里我写的是2020/1/1,通过api读取出来的是2020/01/01(字符串);
//            2,数据库里date类型 显示的是 xxxx-xx-xx , 但是我用2020/01/01这样一个字符串去做等值条件查询还是能够查出来
        if (list!=null&&list.size()!=0){
            ArrayList<OrderSetting> saveOrderSettings = new ArrayList<>();
            ArrayList<OrderSetting> updateOrderSettings = new ArrayList<>();
            for (String[] stringArray : list) {
            QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
                System.out.println(stringArray[0]);
                queryWrapper.eq("orderDate",stringArray[0]);
                OrderSetting orderSetting = baseMapper.selectOne(queryWrapper);
                if (orderSetting==null){
                    OrderSetting saveOrderSetting = new OrderSetting();
                    saveOrderSetting.setOrderDate(DateUtils.parseString2Date(stringArray[0]));
                    saveOrderSetting.setNumber(Integer.parseInt(stringArray[1]));
                    saveOrderSetting.setReservations(0);
                    saveOrderSettings.add(saveOrderSetting);
                }else {
                    OrderSetting updateOrderSetting = new OrderSetting();
                    updateOrderSetting.setId(orderSetting.getId());
                    updateOrderSetting.setOrderDate(DateUtils.parseString2Date(stringArray[0]));
                    updateOrderSetting.setNumber(Integer.parseInt(stringArray[1]));
                    updateOrderSetting.setReservations(0);
                    updateOrderSettings.add(updateOrderSetting);
                }
            }
            // 以下两个方法就是批量插入
            saveBatch(saveOrderSettings);
            updateBatchById(updateOrderSettings);
        }
    }

    @Override
    public List<Map> findOrderSettingsData(String yearAndMonth) {
        String start = yearAndMonth + "-01";
        String end = yearAndMonth + "-31";
        // 需要注意的是此方法配置的sql语句用了一个day(orderdate)数据库函数,用来获取date类型数据的所在月的第几天
        List<Map> list = baseMapper.findOrderSettingsData(start, end);
        return list;
    }

    @Override
    public void updateOrderSettingData(String dateparam, Integer num) {
        // 首先根据日期查出该条数据
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderDate",dateparam);
        OrderSetting orderSetting = baseMapper.selectOne(queryWrapper);
        orderSetting.setNumber(num);
        UpdateWrapper<OrderSetting> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("orderDate",dateparam);
        update(orderSetting,updateWrapper);
    }
}
